﻿using RuneMasterWin8.Common;
using RuneMasterWin8.Core;
using RuneMasterWin8.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “基本页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234237 上有介绍

namespace RuneMasterWin8.Pages
{
    /// <summary>
    /// 基本页，提供大多数应用程序通用的特性。
    /// </summary>
    public sealed partial class ModeSelectPage : RuneMasterWin8.Common.LayoutAwarePage
    {
        public ModeSelectPage()
        {
            this.InitializeComponent();
            DisplayProperties.AutoRotationPreferences = DisplayOrientations.Landscape | DisplayOrientations.LandscapeFlipped;
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            //载入页面
            RefreshGameModeUnlockStatus();

            //根据模式解锁状态来显示
            if (StageManager.Instance.ModeUnlockStatus.ContainsKey("e") && StageManager.Instance.ModeUnlockStatus["e"])
            {
                this.btnEasy.IsEnabled = true;
            }
            else
            {
                this.btnEasy.IsEnabled = false;
            }

            if (StageManager.Instance.ModeUnlockStatus.ContainsKey("n") && StageManager.Instance.ModeUnlockStatus["n"])
            {
                this.btnNormal.IsEnabled = true;
            }
            else
            {
                this.btnNormal.IsEnabled = false;
            }

            if (StageManager.Instance.ModeUnlockStatus.ContainsKey("h") && StageManager.Instance.ModeUnlockStatus["h"])
            {
                this.btnHard.IsEnabled = true;
            }
            else
            {
                this.btnHard.IsEnabled = false;
            }
        }

        /// <summary>
        /// 保留与此页关联的状态，以防挂起应用程序或
        /// 从导航缓存中放弃此页。值必须符合
        /// <see cref="SuspensionManager.SessionState"/> 的序列化要求。
        /// </summary>
        /// <param name="pageState">要使用可序列化状态填充的空字典。</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        /// <summary>
        /// 刷新游戏模式是否被解锁
        /// </summary>
        private void RefreshGameModeUnlockStatus()
        {
            bool isUnlockedNew = false;
            if (!(StageManager.Instance.ModeUnlockStatus.ContainsKey("n") && StageManager.Instance.ModeUnlockStatus["n"]))
            {
                string gameMode = "e";
                int count = 0;
                //查看简单模式下的解锁情况
                for (int i = 1; i <= 10; i++)
                {
                    if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + i)
                            && StageManager.Instance.StageUnlockStatus[gameMode + "_" + i])
                    {
                        count++;
                    }
                }
                if (count >= 6)
                {
                    StageManager.Instance.ModeUnlockStatus["n"] = true;
                    Toast.ShowToast("解锁新难度", "您成功解锁了[普通]模式，快去试试吧");
                    isUnlockedNew = true;
                    ////解锁一个关卡
                    //if (!StageManager.Instance.StageUnlockStatus.ContainsKey("n_1"))
                    //{
                    //    StageManager.Instance.StageUnlockStatus.Add("n_1", true);
                    //    StageManager.Instance.SaveStageUnlockInfo();
                    //}
                }
            }

            if (!(StageManager.Instance.ModeUnlockStatus.ContainsKey("h") && StageManager.Instance.ModeUnlockStatus["h"]))
            {
                string gameMode = "n";
                int count = 0;
                //查看普通模式下的解锁情况
                for (int i = 1; i <= 10; i++)
                {
                    if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + i)
                            && StageManager.Instance.StageUnlockStatus[gameMode + "_" + i])
                    {
                        count++;
                    }
                }
                if (count >= 6)
                {
                    StageManager.Instance.ModeUnlockStatus["h"] = true;
                    Toast.ShowToast("解锁新难度", "您成功解锁了[困难]模式，快去试试吧");
                    isUnlockedNew = true;
                    ////解锁一个关卡
                    //if (!StageManager.Instance.StageUnlockStatus.ContainsKey("h_1"))
                    //{
                    //    StageManager.Instance.StageUnlockStatus.Add("h_1", true);
                    //    StageManager.Instance.SaveStageUnlockInfo();
                    //}
                }
            }
            if (isUnlockedNew)
            {
                StageManager.Instance.SaveModeUnlockInfo();
            }
        }

        private void btnHard_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            AudioPlayHelper.PlayEffectSound("button_click");
            StageManager.Instance.CurrentGameMode = "h";
            // 在此处添加事件处理程序实现。
            this.Frame.Navigate(typeof(StagePage));
        }

        private void btnNormal_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            AudioPlayHelper.PlayEffectSound("button_click");
            StageManager.Instance.CurrentGameMode = "n";
            // 在此处添加事件处理程序实现。
            this.Frame.Navigate(typeof(StagePage));
        }

        private void btnEasy_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            AudioPlayHelper.PlayEffectSound("button_click");
            StageManager.Instance.CurrentGameMode = "e";
            // 在此处添加事件处理程序实现。
            this.Frame.Navigate(typeof(StagePage));
        }
    }
}
